package com.levy.utils.token;

import com.levy.vo.R;
import com.levy.vo.ResultEnum;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.io.ByteArrayOutputStream;
import java.security.Key;
import java.util.Date;

/**
 * Created by levy on 2018/3/21.
 */
public class TokenTest {

    //长度必须20位
    final static String CREATE_KEY = "B_G84kH0rHQPed5T56Sf";

    public static String getTokenStr() {
        String result = EncryptionUtils.encryptString(CREATE_KEY+System.currentTimeMillis());
        return result.replace("+", "%2B");
    }


    public static R checkUrlToken(String client_key) {
        // DES解密
        String UrlToken = EncryptionUtils.decryptString(client_key.replaceAll("%2B", "+"));
        String key = UrlToken.substring(0, 20);
        // 验证Key
        if (CREATE_KEY.equals(key)) {
            return R.ok();
        } else {
            return R.error(ResultEnum.TOKEN_ERROR);
        }
    }


    public static void main(String[] args) {

        //生成token
        String token = getTokenStr();
        System.out.println(token);

        //验证token
        R r = checkUrlToken(token);
        System.out.println(r.toString());

    }
}
